﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class Modules_Personal_TargetAssignOrgView : System.Web.UI.Page
{
    IPerf_Personal_TargetTypeHandler ttHandler = new Perf_Personal_TargetTypeManage();
    IPerf_Personal_FinalAssignHandler finalAssignHandler = new Perf_Personal_FinalAssignManage();
    IPerf_Personal_FinalAssignDtlHandler finalAssignDtlHandler = new Perf_Personal_FinalAssignDtlManage();
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_OrganizationHandler orgHandler = new Sys_OrganizationManage();
    ISys_PostHandler postHandler = new Sys_PostManage();
    IPerf_Personal_TargetOwnerHandler toHandler = new Perf_Personal_TargetOwnerManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private void dataBind()
    {
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                try
                {
                    Perf_Personal_FinalAssignQuery finalAssignQuery = finalAssignHandler.GetPerf_Personal_FinalAssignQuery(ID);
                    if (finalAssignQuery != null)
                    {
                        Perf_Personal_FinalAssignDtlQuery[] finalAssignDtlList = finalAssignDtlHandler.GetPerf_Personal_FinalAssignDtlQueryList(
                            " Final_Assign_ID = @Final_Assign_ID", new SqlParameter[] { new SqlParameter("@Final_Assign_ID", finalAssignQuery.ID) });
                        StringBuilder targetIds = new StringBuilder();
                        if (finalAssignDtlList != null && finalAssignDtlList.Length > 0)
                        {
                            foreach (Perf_Personal_FinalAssignDtlQuery dtlQuery in finalAssignDtlList)
                            {
                                targetIds.Append(dtlQuery.Target_ID);
                                targetIds.Append(",");
                            }
                            targetIds = targetIds.Remove(targetIds.Length - 1, 1);
                        }
                        DataTable targetList = targetHandler.GetPerf_Personal_TargetList(
                            " ID in (" + targetIds.ToString() + ")", new SqlParameter[] {
                            }, " TargetType_ID asc, ID asc");

                        gvTargetList.DataSource = targetList;
                        gvTargetList.DataBind();

                        Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(finalAssignQuery.Target_User_ID);
                        if (userQuery != null)
                            lblUser.Text = userQuery.Name;

                        Sys_OrganizationQuery orgQuery = orgHandler.GetSys_OrganizationQuery(userQuery.Org_ID);
                        if (orgQuery != null)
                            lblOrg.Text = orgQuery.Name;

                        Sys_PostQuery postQuery = postHandler.GetSys_PostQuery(userQuery.Post_ID);
                        if (postQuery != null)
                            lblPost.Text = postQuery.Name;

                        Perf_Personal_TargetOwnerQuery toQuery = toHandler.GetPerf_Personal_TargetOwnerQuery(finalAssignQuery.TargetOwner_ID);
                        if (toQuery != null)
                            lblTargetOwner.Text = toQuery.Name;
                    }
                }
                catch
                {
                    Jscript.Alert("操作失败!", this.Page);
                }
            }
        }
    }

    protected void gvTargetList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                Perf_Personal_TargetTypeQuery ttQuery = ttHandler.GetPerf_Personal_TargetTypeQuery(int.Parse(e.Row.Cells[2].Text));
                if (ttQuery != null)
                {
                    e.Row.Cells[2].Text = ttQuery.Name;
                }

                e.Row.Cells[4].Text = Enums.GetYesNoStatus((Enums.YesNoStatus)int.Parse(e.Row.Cells[4].Text));
            }
        }
    }

}
